//
//  HeadView.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/25.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class HeadView: UIView {
    var scrollView:UIScrollView!//承载图片的scroll
    var pageControll:UIPageControl!//分页小点点
    var imageCount:Int = 6//现在假装有6个banner
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createScrollView()
        self.createHeadImage()
        self.craetePageControll()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:--UI
extension HeadView{
    //创建scrollView
    func createScrollView(){
        scrollView = UIScrollView(frame: self.frame)
        scrollView.delegate = self
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollEnabled = true
        scrollView.pagingEnabled = true
        scrollView.contentSize = CGSize(width: WidthAPP*CGFloat(imageCount), height: 121*SCALEH)
        self.addSubview(scrollView)
    }
    
    
    //创建headerImage
    func createHeadImage(){
        for i in 0..<imageCount{
            let headImage = UIImageView(frame: CGRect(x: WidthAPP*CGFloat(i), y: 0, width: WidthAPP, height: 121*SCALEH))
            headImage.backgroundColor = BaseFunc.createARCColor()
            scrollView.addSubview(headImage)
        }
    }
    
    
    //创建pageControll
    func craetePageControll(){
        pageControll = UIPageControl(frame: CGRect(x: 0,y: 107*SCALEH,width: WidthAPP,height: 7))
        pageControll.numberOfPages = imageCount
        pageControll.currentPage = 0
        pageControll.pageIndicatorTintColor = Theme.BarGrayColor
        pageControll.currentPageIndicatorTintColor = UIColor.whiteColor()
        pageControll.hidesForSinglePage = true
        pageControll.addTarget(self, action: #selector(self.changedBannerPage(_:)), forControlEvents: UIControlEvents.ValueChanged)
        self.addSubview(pageControll)
    }
}


//MARK:--点击事件
extension HeadView{
    //实现pageControl的点击事件
    func changedBannerPage(sender:UIPageControl){
        let page:CGFloat = CGFloat(pageControll.currentPage)
        print(page)
        self.scrollView.scrollRectToVisible(CGRect(x: WidthAPP*page, y: 0, width: WidthAPP, height: 121*SCALEH), animated: true)
    }
}

//MARK:UIScrollView  delegate
extension HeadView:UIScrollViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let pageNum = self.scrollView.contentOffset.x/WidthAPP
        self.pageControll.currentPage = Int(pageNum)
    }
}
















